using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace WozLib.UI.Ink.SketchCommands
{
  public class RememberSketchCommand : SketchCommand
  {
    [XmlAttribute("WozClientClearCommandID")]
    public long WozClientClearCommandID
    {
      get
      {
        return _WozClientClearCommandID;
      }
      set
      {
        _WozClientClearCommandID = value;
      }
    } long _WozClientClearCommandID = long.MinValue;

    // we need these to be static so that we don't get collision
    private static long _NextServerID = 0;

    private static long _NextClientID = 10000000;

    private static Dictionary<long, RememberSketchCommand> IdToCommand = new Dictionary<long, RememberSketchCommand>();

    protected void RegisterObject()
    {
      if (WozClientClearCommandID != long.MinValue)
      {
        IdToCommand[WozClientClearCommandID] = this;
      }
      else
      {
        long id = long.MinValue;

        // lock on Woz Object Dictionary so we can prevent handing out duplicate IDs
        lock (IdToCommand)
        {
          // Get the next ID
          id = _NextServerID;

          // Increment the next ID
          _NextServerID++;
        }

        WozClientClearCommandID = id;
        IdToCommand[WozClientClearCommandID] = this;
      }
    }

    protected RememberSketchCommand GetCommand(long id)
    {
      if (IdToCommand.ContainsKey(WozClientClearCommandID))
      {
        RememberSketchCommand wccc = IdToCommand[WozClientClearCommandID];
        return wccc;
      }
      else
      {
        return null;
      }
    }
  }
}
